﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Applee.FractCommon;
using System.Drawing;

namespace Applee.PentagramFract
{
    public class Pentagram : BaseShape
    {
        public Pentagram()
        {
            points = new PointF[11] 
                { 
                    new PointF(), 
                    new PointF(),
                    new PointF(),                     
                    new PointF(), 
                    new PointF(), 
                    new PointF(),
                    new PointF(), 
                    new PointF(),              
                    new PointF(), 
                    new PointF(),
                    new PointF() 
                };
        }

        public override void DoMakeShape(System.Drawing.PointF point, float fL)
        {
            float fLength = -fL;
            float lctAngle = (float)(18.0F * Math.PI / 180);
            float lcbAngle = (float)(36.0F * Math.PI / 180);

            //R = L (cos18 + sin18 / tan36)
            float fRad = fLength * ((float)Math.Cos(lctAngle) + (float)Math.Sin(lctAngle) / (float)Math.Tan(lcbAngle));

            //R' = L sin18 / sin36
            float fRadShort = fLength * ((float)Math.Sin(lctAngle) / (float)Math.Sin(lcbAngle));

            //H = R x (1 + cos36)            
            float fH = fRad - fRad * (1 + (float)Math.Sin(lcbAngle)) * 0.5F;
            fH *= 0.5F;

            //(0, R)
            points[0] = new PointF(point.X, point.Y + fRad - fH);

            //(Lsin18, Lsin18 / tan36)
            points[1] = new PointF(point.X + fLength * (float)Math.Sin(lctAngle),
                point.Y + fLength * (float)Math.Sin(lctAngle) / (float)Math.Tan(lcbAngle) - fH);

            //(Rcos18, Rsin18)
            points[2] = new PointF(point.X + fRad * (float)Math.Cos(lctAngle), point.Y + fRad * (float)Math.Sin(lctAngle) - fH);

            //(R'cos18, -R'sin18)
            points[3] = new PointF(point.X + fRadShort * (float)Math.Cos(lctAngle), point.Y - fRadShort * (float)Math.Sin(lctAngle) - fH);

            //(Rsin36, -Rcos36)
            points[4] = new PointF(point.X + fRad * (float)Math.Sin(lcbAngle), point.Y - fRad * (float)Math.Cos(lcbAngle) - fH);

            //(0, -Lsin18 / sin36)
            points[5] = new PointF(point.X, point.Y - fLength * (float)Math.Sin(lctAngle) / (float)Math.Sin(lcbAngle) - fH);

            //(-Rsin36, -Rcos36)
            points[6] = new PointF(point.X - fRad * (float)Math.Sin(lcbAngle), point.Y - fRad * (float)Math.Cos(lcbAngle) - fH);

            //(-R'cos18, -R'sin18)
            points[7] = new PointF(point.X - fRadShort * (float)Math.Cos(lctAngle), point.Y - fRadShort * (float)Math.Sin(lctAngle) - fH);

            //(-Rcos18, Rsin18)
            points[8] = new PointF(point.X - fRad * (float)Math.Cos(lctAngle), point.Y + fRad * (float)Math.Sin(lctAngle) - fH);
            points[9] = new PointF(point.X - fLength * (float)Math.Sin(lctAngle),
                point.Y + fLength * (float)Math.Sin(lctAngle) / (float)Math.Tan(lcbAngle) - fH);
            points[10] = points[0];

        }

        internal void MakeShape(PointF pointCenter, PointF pointOnEdge, float fLength)
        {
            MakeShape(pointCenter, fLength);
            PointF point = points[0];
            float fa = MathUtils.AngleFromP2P(pointCenter, point, pointOnEdge);

            foreach (PointF p in points)
                MathUtils.RotatePointRad(pointCenter, p, fa);
        }
    }
}
